﻿namespace ShortcutReader
{
    using System.ComponentModel;
    using System.Windows.Input;

    using IWshRuntimeLibrary;

    using ShortcutReader.Helpers;

    public class MainViewModel : INotifyPropertyChanged
    {
        #region Fields

        private readonly LogHelper Logger;

        private string arguments;
        private string shortcutPath;
        private string statusLog = "Ready...";
        private string targetPath;

        #endregion Fields

        #region Constructors

        public MainViewModel()
        {
            this.Logger = new LogHelper(this);
            this.SaveShortcutCommand = new RelayCommand(e => this.SaveShortcut(), e => true);
        }

        #endregion Constructors

        #region Events

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion Events

        #region Properties

        public string Arguments
        {
            get { return this.arguments; }
            set
            {
                this.arguments = value;
                this.OnPropertyChanged("Arguments");
            }
        }

        public ICommand SaveShortcutCommand
        {
            get;
            private set;
        }

        public string ShortcutPath
        {
            get { return this.shortcutPath; }
            set
            {
                this.shortcutPath = value;
                this.OnPropertyChanged("ShortcutPath");
            }
        }

        public string StatusLog
        {
            get { return this.statusLog; }
            set
            {
                this.statusLog = value;
                this.OnPropertyChanged("StatusLog");
            }
        }

        public string TargetPath
        {
            get { return this.targetPath; }
            set
            {
                this.targetPath = value;
                this.OnPropertyChanged("TargetPath");
            }
        }

        #endregion Properties

        #region Methods

        public void OpenShortcut()
        {
            this.Logger.LogShortcutPath();

            var link = GetWshShortcut();

            this.TargetPath = link.TargetPath;
            this.Arguments = link.Arguments;

            this.Logger.LogStatus("Opened");
        }

        public void SaveShortcut()
        {
            this.Logger.LogShortcutPath();

            var link = this.GetWshShortcut();

            link.TargetPath = this.TargetPath;
            link.Arguments = this.Arguments;

            link.Save();

            this.Logger.LogStatus("Saved");
        }

        private IWshShortcut GetWshShortcut()
        {
            WshShell shell = new WshShell();
            var link = (IWshShortcut)shell.CreateShortcut(this.ShortcutPath);
            return link;
        }

        private void OnPropertyChanged(string property)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }

        #endregion Methods
    }
}